<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList"
          >
            <el-select
              v-model="queryInfo.queryType"
              slot="prepend"
              placeholder="请选择"
              style="width: 110px"
            >
              <el-option label="订单编号" :value="0"></el-option>
              <el-option label="下单用户" :value="1"></el-option>
            </el-select>
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单数据页面 -->
      <el-table :data="orderList" border stripe>
        <!-- 展开行列 -->
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-descriptions border>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"></i>
                  用户名
                </template>
                {{ props.row.userName }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-mobile-phone"></i>
                  手机号
                </template>
                {{ props.row.telephoneNumber }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-location-outline"></i>
                  收货人
                </template>
                {{ props.row.consignee }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-tickets"></i>
                  备注
                </template>
                <el-tag size="small">{{ props.row.remarks }}</el-tag>
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-office-building"></i>
                  收货地址
                </template>
                {{ props.row.deliveryAddress }}
              </el-descriptions-item>
            </el-descriptions>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="orderNumber"></el-table-column>
        <el-table-column
          label="订单价格"
          prop="paymentAmount"
          width="110px"
          sortable
        ></el-table-column>
        <el-table-column
          label="是否付款"
          prop="settlementStatus"
          width="110px"
          sortable
        >
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.settlementStatus === false"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="支付方式"
          prop="paymentName"
          width="110px"
          sortable
        >
        </el-table-column>
        <el-table-column
          label="是否发货"
          prop="deliveryStatus"
          width="110px"
          sortable
        >
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.deliveryStatus === false"
              >未发货</el-tag
            >
            <el-tag type="success" v-else>已发货</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="下单用户"
          prop="userName"
          sortable
        ></el-table-column>
        <el-table-column
          label="下单时间"
          prop="createdTime"
          sortable
        ></el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="120px">
          <!-- 修改地址按钮 -->
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="showBox"
          ></el-button>
          <!-- 物流进度按钮 -->
          <el-button
            size="mini"
            type="success"
            icon="el-icon-location"
            @click="progressShowBox"
          ></el-button>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageIndex"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改地址输入框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="AddressVisible"
      width="50%"
      @close="addressDialogClose"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="CityData"
            v-model="addressForm.address1"
            clearable
            filterable
            :props="{ expandTrigger: 'hover' }"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="AddressVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 展示物流进度的对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
// 导入文件
import CityData from '../../assets/js/citydata'
export default {
  data () {
    return {
      // 指定节点排序方向
      reverse: true,
      // 时间线数据
      activities: [
        {
          content: '顺丰速运 已收取快件',
          timestamp: '2021-08-16 19:53'
        },
        {
          content: '快件在【广州天河石牌西营业部】完成分拣,准备发往 【广州穗佳陆运中转场】',
          timestamp: '2021-08-16 21:22'
        },
        {
          content: '快件已发车',
          timestamp: '2021-08-16 21:22'
        },
        {
          content: '快件到达 【广州穗佳陆运中转场】',
          timestamp: '2021-08-16 22:36'
        },
        {
          content: '快件在【广州穗佳陆运中转场】完成分拣,准备发往 【厦门杏林中转场】',
          timestamp: '2021-08-17 05:15'
        },
        {
          content: '快件已发车',
          timestamp: '2021-08-17 05:36'
        },
        {
          content: '快件到达 【厦门杏林中转场】',
          timestamp: '2021-08-17 14:14'
        },
        {
          content: '快件在【厦门杏林中转场】完成分拣,准备发往 【龙岩新罗集散点】',
          timestamp: '2021-08-17 14:22'
        },
        {
          content: '快件已发车',
          timestamp: '2021-08-17 15:31'
        },
        {
          content: '快件到达 【龙岩新罗集散点】',
          timestamp: '2021-08-17 18:32'
        },
        {
          content: '快件在【龙岩新罗集散点】完成分拣,准备发往 【龙岩曹溪营业点】',
          timestamp: '2021-08-17 19:50'
        },
        {
          content: '快件已发车',
          timestamp: '2021-08-17 19:50'
        },
        {
          content: '快件到达 【龙岩曹溪营业点】',
          timestamp: '2021-08-17 20:20'
        },
        {
          content: '快件交给黄炎榕,正在派送途中（联系电话：17720986663,顺丰已开启“安全呼叫”保护您的电话隐私,请放心接听！）（主单总件数：1件）',
          timestamp: '2021-08-18 08:15'
        },
        {
          content: '您的快件已签收，如有疑问请电联快递员【黄炎榕，电话：17720986663】。疫情期间顺丰每日对网点消毒、快递员每日测温、配戴口罩，感谢您使用顺丰，期待再次为您服务。（主单总件数：1件）',
          timestamp: '2021-08-18 09:14'
        },
      ],
      // 订单列表查询的参数
      queryInfo: {
        // 查询条件
        query: '',
        // 显示页码
        pageIndex: 1,
        // 显示条数
        pageSize: 10,
        // 搜索的类型
        queryType: 0
      },
      // 订单总数
      total: 0,
      // 订单列表数据
      orderList: [],
      // 地址对话框的显示与隐藏
      AddressVisible: false,
      // 物流管理对话框的显示与隐藏
      progressVisible: false,
      // 城市数据
      CityData,
      // 物流管理数据
      progressInfo: [],
      // 地址表单的数据绑定对象
      addressForm: {
        // 省市县
        address1: [],
        // 详细地址
        address2: ''
      },
      // 地址的表单验证规则
      addressFormRules: {
        address1: [
          { required: true, message: '请选择地址', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // 获取订单列表
    this.getOrderList()
  },
  methods: {
    // 获取订单列表
    async getOrderList () {
      const { data: res } = await this.$http.get('CommodityOrders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取数据列表失败')
      } else {
        console.log(res.data);
        // 订单总条数
        this.total = res.data.pager.pageTotal
        // 订单列表
        this.orderList = res.data.commodityOrders
      }
    },
    // 分页处理函数
    handleSizeChange (newSise) {
      this.queryInfo.pageSize = newSise
      this.getOrderList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pageIndex = newPage
      this.getOrderList()
    },
    // 显示修改地址对话框
    showBox () {
      this.AddressVisible = true
    },
    // 关闭后清空表单
    addressDialogClose () {
      this.$refs.addressFormRef.resetFields()
    },
    // 显示物流对话框
    progressShowBox () {
      this.progressVisible = true
    }
  }
}
</script>

<style scoped>
.el-cascader {
  width: 100%;
}
</style>
